package com.ftm.dao.impl;

import java.util.List;

import com.ftm.tools.DBTools;
import com.ftm.tools.PageData;
import com.ftm.dao.GoodsDao;
import com.ftm.entity.Goods;
import com.ftm.entity.Shop;

public class GoodsDaoImpl implements GoodsDao {
	/**
	 * 增加商品 Goods 商品对象
	 */
	@Override
	public boolean addGoods(Goods goods) {
		String sql = "insert into ftm_goods values(null,?,?,?,?,?,?,?,?)";
		return DBTools.exUpdate(sql, goods.getGoodsname(), goods.getGoodsprice(), goods.getGoodsinfo(),
				goods.getGoodscount(), goods.getCover(), goods.getTypeid(), goods.getShopid(),
				goods.getGoodsstate()) > 0;
	}

	/**
	 * 删除商品
	 * 
	 */
	@Override
	public boolean delGoods(int goodsid) {
		String sql = "delete from ftm_goods where goodsid=?";
		return DBTools.exUpdate(sql, goodsid) > 0;
	}

	/**
	 * 修改商品信息
	 */
	@Override
	public boolean upGoods(String goodsname, double goodsprice, String goodsinfo, int goodscount, String cover,
			int typeid, int shopid, int goodsstate, int goodsid) {

		String sql = "update ftm_goods set goodsname =?,goodsprice=?,goodsinfo=?,goodscount=?,cover=?,typeid=?,shopid=?,goodsstate=? where goodsid=?";
		return DBTools.exUpdate(sql, goodsname, goodsprice, goodsinfo, goodscount, cover, typeid, shopid, goodsstate,
				goodsid) > 0;
	}

	/**
	 * 修改商品状态 上下架
	 */
	@Override
	public boolean updateGoods(int goodsid, int goodsstate) {
		String sql = "update ftm_goods set goodsstate =? where goodsid=?";
		return DBTools.exUpdate(sql, goodsstate, goodsid) > 0;
	}

	/**
	 * 搜索商品名称 (模糊查询)
	 */
	@Override
	public PageData<Goods> queryGoods(int page, int pageSize, String keywords) {
		String sql = "select goodsid,goodsname,goodsprice,goodsinfo,goodscount,goodsstate,cover,shopid from ftm_goods where goodsname like ?";
		return DBTools.exQueryByPage(sql, Goods.class, page, pageSize, '%' + keywords + '%');
	}

	/**
	 * 搜索商品名称 (模糊查询)
	 */
	@Override
	public PageData<Goods> queryGoodsShopLike(int page, int pageSize, int shopid, String keywords) {
		String sql = "select ftm_goods.goodsid,ftm_goods.goodsname,ftm_goods.goodsprice,ftm_goods.goodsinfo,ftm_goods.goodscount,ftm_goods.cover,ftm_goods.shopid,ftm_goods.goodsstate,ftm_goodstype.typeid,ftm_goodstype.typename,ftm_goodstype.typeinfo from ftm_goods inner join ftm_goodstype on ftm_goods.typeid=ftm_goodstype.typeid \r\n"
				+ " where (ftm_goods.goodsname like ? or ftm_goods.goodsinfo like ?) and ftm_goods.shopid=?";
		return DBTools.exQueryByPage(sql, Goods.class, page, pageSize, '%' + keywords + '%', '%' + keywords + '%',
				shopid);
	}

	/**
	 * 
	 * 商品类别查询(商品类别id)
	 */
	@Override
	public PageData<Goods> queryGoodsType(int page, int pageSize, int typeid) {
		String sql = "select * from ftm_goods where typeid=?";
		return DBTools.exQueryByPage(sql, Goods.class, page, pageSize, typeid);
	}

	/**
	 * 商铺的所以商品(根据商店id)
	 */
	@Override
	public PageData<Goods> queryGoodsShopid(int page, int pageSize, int shopid) {
		String sql = "select ftm_goods.goodsid,ftm_goods.goodsname,ftm_goods.goodsprice,ftm_goods.goodsinfo,ftm_goods.goodscount,ftm_goods.cover,ftm_goods.shopid,ftm_goods.goodsstate,ftm_goodstype.typeid,ftm_goodstype.typename,ftm_goodstype.typeinfo from ftm_goods inner join ftm_goodstype on ftm_goods.typeid=ftm_goodstype.typeid where ftm_goods.shopid=?";
		return DBTools.exQueryByPage(sql, Goods.class, page, pageSize, shopid);
	}

	/**
	 * 查询商品详细信息(商品详情)
	 */
	@Override
	public Goods queryOneGoods(int goodsid) {
		String sql = "select * from ftm_goods where goodsid = ?";
		List<Goods> list = (List<Goods>) DBTools.exQuery(sql, Goods.class, goodsid);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
	/**
	 * 查询所有商品表
	 */
	@Override
	public List<Goods> queryAllGoods() {
		String sql = "select * from ftm_goods";
		List<Goods> list = (List<Goods>) DBTools.exQuery(sql, Goods.class);
		return list;
	}
	
}
